'use strict';

    angular.module('app')
        .controller('menuUpdateController', ['$scope', '$rootScope', '$state', '$timeout', 'toaster',
                                             function($scope, $rootScope, $state, $timeout, toaster) {
        var title = "";
        var defaultAva = $rootScope.defaultAvatar;
        $scope.myImage='';
        // 图标的集合，后面还可以继续添加
        $scope.sites = [
            {name : "glyphicon fui-video", url : "glyphicon fui-video"},
            {name : "glyphicon fui-time", url : "glyphicon fui-time"},
            {name : "glyphicon fui-settings", url : "glyphicon fui-settings"},
            {name : "glyphicon fui-new", url : "glyphicon fui-new"},
            {name : "glyphicon fui-menu", url : "glyphicon fui-menu"},
            {name : "glyphicon fui-man", url : "glyphicon fui-man"},
            {name : "glyphicon fui-mail", url : "glyphicon fui-mail"},
            {name : "glyphicon fui-lock", url : "glyphicon fui-lock"},
            {name : "glyphicon fui-location", url : "glyphicon fui-location"},
            {name : "glyphicon fui-eye", url : "glyphicon fui-eye"},
            {name : "glyphicon fui-cross", url : "glyphicon fui-cross"},
            {name : "glyphicon fui-cmd", url : "glyphicon fui-cmd"},
            {name : "glyphicon fui-checkround", url : "glyphicon fui-checkround"},
            {name : "glyphicon fui-heart", url : "glyphicon fui-heart"},
            {name : "glyphicon fui-checkmark", url : "glyphicon fui-checkmark"},
            {name : "glyphicon fui-calendar", url : "glyphicon fui-calendar"},
            {name : "glyphicon fui-bubble", url : "glyphicon fui-bubble"},
            {name : "glyphicon fui-volume", url : "glyphicon fui-volume"},
            {name : "glyphicon glyphicon-user", url : "glyphicon glyphicon-user"},
            {name : "glyphicon glyphicon-random", url : "glyphicon glyphicon-random"},
            {name : "glyphicon glyphicon-stats", url : "glyphicon glyphicon-stats"},
            {name : "glyphicon glyphicon-list", url : "glyphicon glyphicon-list"},
            {name : "glyphicon glyphicon-book", url : "glyphicon glyphicon-book"},
            {name : "glyphicon glyphicon-bookmark", url : "glyphicon glyphicon-bookmark"},
            {name : "glyphicon glyphicon-tags", url : "glyphicon glyphicon-tags"},
            {name : "glyphicon glyphicon-cog", url : "glyphicon glyphicon-cog"},
            {name : "glyphicon glyphicon-equalizer", url : "glyphicon glyphicon-equalizer"},
            {name : "glyphicon glyphicon-file", url : "glyphicon glyphicon-file"},
            {name : "glyphicon glyphicon-flag", url : "glyphicon glyphicon-flag"},
            {name : "glyphicon glyphicon-earphone", url : "glyphicon glyphicon-earphone"},
            {name : "glyphicon glyphicon-wrench", url : "glyphicon glyphicon-wrench"},
            {name : "glyphicon glyphicon-signal", url : "glyphicon glyphicon-signal"},
            {name : "glyphicon glyphicon-lock", url : "glyphicon glyphicon-lock"},
            {name : "glyphicon fui-link", url : "glyphicon fui-link"},
            {name : "glyphicon glyphicon-fire", url : "glyphicon glyphicon-fire"},
            {name : "glyphicon glyphicon-road", url : "glyphicon glyphicon-road"},
            {name : "glyphicon glyphicon-bullhorn", url : "glyphicon glyphicon-bullhorn"}

            ];

        // $scope.myCroppedImage=$scope.myCroppedImage ;
        $scope.myCroppedImage = '';
        if($state.includes('**.menu.update')){
            title="编辑菜单";
            var id = $state.params.id;
            selectParent(null);
            activate(id);
            validate(id);
        }else if($state.includes('**.menu.create')){
            title="添加菜单";
            validate(null);
            selectParent(null);
            setTimeout(function(){
                $scope.myCroppedImage = defaultAva;
                !$rootScope.$$phase && $scope.$apply();
            },300);

        }
        $scope.title = $rootScope.title = title;
        $scope.loading = true;
        //初始化验证
        //validate($scope);
        $scope.submit= function(){
            var m = $scope.record;
            var menuType = $("#menuType").val();
            if (menuType != 2) {
            	$scope.record.parentId = 0;
            }
            if(m){
                $scope.isDisabled = true;//提交disabled
                $.ajax({
    				url : $scope.record.id ? $scope.app.project_name+'/menu/update' : $scope.app.project_name+'/menu/add',
    				data: $scope.record
    			}).then(callback);
            }
            function callback(result) {
                if(result.httpCode ==200){//成功
                    toaster.clear('*');
                    toaster.pop('success', '', "保存成功");
                    $timeout(function(){
                        $state.go('main.sys.menu.list');
                    },2000);
                }else{
                    toaster.clear('*');
                    toaster.pop('error', '', result.msg);
                    $scope.isDisabled = false;
                }
            }
        }

        var handleFileSelect=function(evt) {
            var file=evt.currentTarget.files[0];
            if(!/image\/\w+/.test(file.type)){
                return false;
            }
            var reader = new FileReader();
            reader.onload = function (evt) {
                $scope.$apply(function($scope){
                    $scope.myImage=evt.target.result;
                });
            };
            reader.readAsDataURL(file);
        };
        angular.element(document.querySelector('#fileInput')).on('change',handleFileSelect);


        // 初始化页面
        function activate(id) {
	        $scope.loading = true;
        	$.ajax({
				url : $scope.app.project_name+'/menu/read/detail',
				data: {'id': id}
			}).then(function(result) {
		        $scope.loading = false;
		        //$scope.record = result.data;
		        $scope.menuTypeList=[
		        	{
			        	'id' : 1,
			        	'name' : '一级菜单'
		        	},
		        	{
			        	'id' : 2,
			        	'name' : '二级菜单'
		        	}
		        ];
				if (result.httpCode == 200) {
					$scope.record = result.data;
				} else {
					$scope.msg = result.msg;
				}
				$scope.$apply();
			});
        }
        
        // 父类列表

        function selectParent(id) {

            $scope.loading = true;
            $.ajax({
                url : $scope.app.project_name+ '/menu/selParentNameList',
                data: {'id': id}
            }).then(function(result) {
                $scope.loading = false;
                if (result.httpCode == 200) {
                    $scope.parentList = result.data;
                } else {
                    $scope.msg = result.msg;
                }
                $scope.$apply();
            });
        }
        
        // 菜单类型
        function selectMenuType(id) {
            $scope.loading = true;
            $.ajax({
                url : $scope.app.project_name+ '/menu/selmenuTypeList',
                data: {'id': id}
            }).then(function(result) {
                $scope.loading = false;
                if (result.httpCode == 200) {
                    $scope.parentList = result.data;
                } else {
                    $scope.msg = result.msg;
                }
                $scope.$apply();
            });
        }

        //表单验证
        function validate(userId){
            jQuery('form').validate({
                rules: {
                	menuName: {
                        required: true,
                        stringCheck:[],
                        maxLengthB:[20]
                    },
                    menuType: {
                        required: true
                    },
                    request: {
                        required: true
                    },
                    iconcls: {
                        required: true
                    },
                    permission: {
                        required: true
                    },
                    parentId: {
                        required: true
                    }
                },
                messages: {
                	menuName: {
                        required: '请填写菜单名称',
                        maxLengthB:"菜单名称不得超过{0}个字符"
                    }
                },
                submitHandler: function() {
                    $scope.submit();
                }
            });
        }
        // 点击显示图标
        $scope.show = function(){
            $scope.firstApply = true;
        }
        // 得到图标的地址显示
        $scope.get=function (url) {
            $scope.record.iconcls=url;
            $scope.firstApply = false;//将弹框隐藏掉
            $("#clickMe").hide();
        }

    }]);